﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using SevenZip;

namespace JTS_Archiver
{
    public partial class compress : Form
    {
        public compress(Form1 parent)
        {
            InitializeComponent();
            owner = parent;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.A))
            {
                foreach (ListViewItem item in listView1.Items)
                {
                    item.Selected = true;
                }
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }


        public Form1 owner;
        public long sizeTotal = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = saveFileDialog1.FileName;
            }
        }

        public OutArchiveFormat indexToFormat()
        {
            OutArchiveFormat outf;
            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    outf = OutArchiveFormat.SevenZip;
                    break;
                case 1:
                    outf = OutArchiveFormat.Zip;
                    break;
                case 2:
                    outf = OutArchiveFormat.BZip2;
                    break;
                case 3:
                    outf = OutArchiveFormat.GZip;
                    break;
                case 4:
                    outf = OutArchiveFormat.XZ;
                    break;
                case 5:
                    outf = OutArchiveFormat.Tar;
                    break;
                default:
                    outf = OutArchiveFormat.SevenZip;
                    break;
            }
            return outf;
        }

        public CompressionLevel levelToFormat()
        {
            CompressionLevel outf;
            switch (comboBox2.SelectedIndex)
            {
                case 0:
                    outf = CompressionLevel.Ultra;
                    break;
                case 1:
                    outf = CompressionLevel.High;
                    break;
                case 2:
                    outf = CompressionLevel.Normal;
                    break;
                case 3:
                    outf = CompressionLevel.Low;
                    break;
                case 4:
                    outf = CompressionLevel.Fast;
                    break;
                case 5:
                    outf = CompressionLevel.None;
                    break;
                default:
                    outf = CompressionLevel.Ultra;
                    break;
            }
            return outf;
        }

        private void compress_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                listView1.BeginUpdate();
                string[] newfiles = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*", SearchOption.AllDirectories);
                foreach (string newfile in newfiles)
                {
                    FileInfo fi = new FileInfo(newfile);
                    ListViewItem lvi = new ListViewItem(newfile);
                    lvi.SubItems.Add(owner.fSize((ulong)fi.Length));
                    sizeTotal = sizeTotal + fi.Length;
                    listView1.Items.Add(lvi);
                }
                listView1.EndUpdate();
                updateTotals();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                listView1.BeginUpdate();
                foreach (string newfile in openFileDialog1.FileNames)
                {
                    //
                    FileInfo fi = new FileInfo(newfile);
                    ListViewItem lvi = new ListViewItem(newfile);
                    lvi.SubItems.Add(owner.fSize((ulong)fi.Length));
                    sizeTotal = sizeTotal + fi.Length;
                    listView1.Items.Add(lvi);
                }
                listView1.EndUpdate();
                updateTotals();
            }
        }

        public void updateTotals()
        {
            label1.Text = listView1.Items.Count.ToString() + " files in " + owner.fSize((ulong)sizeTotal);
        }

        private void openFileDialog2_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem itm in listView1.SelectedItems)
            {
                FileInfo fi = new FileInfo(itm.Text);
                sizeTotal = sizeTotal - fi.Length;
                itm.Remove();
            }
            updateTotals();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("You need to specify an output file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox1.Focus();
            }
            else
            {
                if (listView1.Items.Count < 1)
                {
                    MessageBox.Show("You need to add some files to archive!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    List<string> fList = new List<string>();
                    foreach (ListViewItem itm in listView1.Items)
                    {
                        fList.Add(itm.Text);
                    }
                    owner.beginCompression(textBox1.Text, indexToFormat(), levelToFormat(), fList.ToArray());
                    Close();
                }
            }
        }
    }
}
